#include "CTranslateResultWidget.h"
#include "ui_CTranslateResultWidget.h"

CTranslateResultWidget::CTranslateResultWidget(QWidget* parent)
    : QWidget(parent),
      ui(new Ui::CTranslateResultWidget)
{
    ui->setupUi(this);

    // 设置窗口关闭时不释放
    setAttribute(Qt::WA_DeleteOnClose, false);
}

CTranslateResultWidget::~CTranslateResultWidget()
{
    delete ui;
}

void CTranslateResultWidget::SetItemCount(int count)
{
    int nSize = m_list.size();

    // 已有数量小于设置数量，则创建控件，已有数量大于设置数量，则隐藏控件
    if (nSize >= count) {
        for (int i = 0; i < nSize; ++i) {
            if (i < count) {
                m_list.at(i)->RefreshWidget();
                m_list.at(i)->show();
            }
            else {
                m_list.at(i)->hide();
            }
        }
    }
    else {
        for (int i = nSize; i < count; ++i) {
            CTranslateResultItemWidget* pWidget = new CTranslateResultItemWidget(this);
            ui->mainHLayout->addWidget(pWidget);
            m_list.append(pWidget);
        }
    }

    this->resize(count * 200, 300);
}

void CTranslateResultWidget::AppendItemText(int index, const QString& text)
{
    if (index >= m_list.size()) {
        return;
    }

    m_list.at(index)->AppendText(text);
}
